package com.sh.personnel.security.config;

import com.sh.personnel.security.IUserDetailServiceImpl;
import com.sh.personnel.security.config.filter.JwtAuthenticationTokenFilter;
import com.sh.personnel.security.config.filter.RegisterCodeAuthenticationFilter;
import com.sh.personnel.security.config.handler.MyAccessDeniedHandler;
import com.sh.personnel.security.config.handler.MyAuthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import com.sh.personnel.security.config.filter.LoginCaptchaAuthenticationFilter;

/**
 * security自定义配置
 * @author 林思浩
 * @date 2020/01/04 18:28
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    IUserDetailServiceImpl userDetailService;

    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    LoginCaptchaAuthenticationFilter loginCaptchaAuthenticationFilter;

    @Autowired
    RegisterCodeAuthenticationFilter registerCodeAuthenticationFilter;

    /**
     * 认证用户
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService)
                .passwordEncoder(passwordEncoder());
    }


    /**
     * 配置策略
     * @param httpSecurity
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity
                .csrf().disable() //不需要csrf
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) //不需要session
                .and()
                .authorizeRequests() //过滤请求
                .antMatchers("/login").anonymous()
                .antMatchers("/register/**").anonymous()
                .antMatchers("/captcha").anonymous()
                .antMatchers(HttpMethod.GET, "/*.html")
                .permitAll() //放行
                .anyRequest().authenticated(); //其余请求都要认证

        //添加过滤器
        httpSecurity
                .addFilterBefore(loginCaptchaAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) //captcha验证
                .addFilterBefore(registerCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) //email验证
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); //jwt验证

        //添加自定义异常处理类
        httpSecurity
                .exceptionHandling()
                .authenticationEntryPoint(new MyAuthEntryPoint()) //未登录会跳转至此返回自定义信息
                .accessDeniedHandler(new MyAccessDeniedHandler()); //无权限会跳转至此返回自定义信息

    }

    /**
     * 密码加密方式
     * @return
     */
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * bean一个AuthenticationManager
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
